package com.bfb.helpers;

import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.bfb.Main;

/**
 * Helper class for functionalities related to the dish types that require
 * server contact.
 * 
 * @author Rawan Khalil (rawan.tkhalil@gmail.com)
 * 
 */
public class DishTypes {

	private static ArrayList<String> dishTypeNames = null;

	/**
	 * Contacts the server by using an {@link HttpGet} with the url path being
	 * "/dish_types/all". The response is received in an {@link HttpResponse}
	 * that is a {@link JSONArray} containing the list of all the dish types in
	 * the db. The result is returned as an {@link ArrayList} of all the dish
	 * type names. The server call is only made if the dish type list has not
	 * been previously retrieved because the dish types are static. This will
	 * reduce the load on the server and make the app faster with less server
	 * calls.
	 * 
	 * @return {@link ArrayList}
	 * 
	 * @author Rawan Khalil (rawan.tkhalil@gmail.com)
	 */
	public static ArrayList<String> getAll() {
		if (dishTypeNames == null) {
			dishTypeNames = new ArrayList<String>();
			try {
				DefaultHttpClient httpClient = new DefaultHttpClient();
				HttpGet httpGet = new HttpGet("http://" + Main.SERVER
						+ "/dish_types/all");
				HttpResponse response = httpClient.execute(httpGet);
				if (response != null) {
					HttpEntity entity = response.getEntity();
					JSONArray jsonArray = new JSONArray(
							ServerUtilities.getContentFromStream(entity
									.getContent()));
					for (int i = 0; i < jsonArray.length(); i++) {
						JSONObject jsonObject = jsonArray.getJSONObject(i);
						dishTypeNames.add(jsonObject.getString("name"));
					}
				} else
					throw new Exception("Response NULL");
			} catch (Exception e) {
				Log.e("DishTypes_GetAll", e.getMessage(), e);
			}
		}
		return dishTypeNames;
	}

	/**
	 * Contacts the server by using an {@link HttpPost} with the url path being
	 * "/dish_types/get_key" to retrieve the key of an dish type by its name.
	 * The name is posted in a list of {@link NameValuePair}. The response is
	 * then retrieved and returned.
	 * 
	 * @param name
	 *            The name of the dish type
	 * 
	 * @return String
	 * 
	 * @author Rawan Khalil (rawan.tkhalil@gmail.com)
	 */
	public static String getKeyByName(final String name) {
		String key = "";
		try {
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost("http://" + Main.SERVER
					+ "/dish_types/get_key");

			ArrayList<NameValuePair> nvpList = new ArrayList<NameValuePair>();
			nvpList.add(new BasicNameValuePair("dishTypeName", name));
			httppost.setEntity(new UrlEncodedFormEntity(nvpList));

			HttpResponse response = httpClient.execute(httppost);

			if (response != null) {
				HttpEntity entity = response.getEntity();
				key = ServerUtilities.getContentFromStream(entity.getContent());
			} else
				throw new Exception("Response NULL");
		} catch (Exception e) {
			Log.e("Dish Type ID Request", e.getMessage(), e);
		}

		return key;
	}

}
